/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 * 02111-1307, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package l1j.server.server.datatables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import l1j.opqlo.Server.L1World.L1World;
import l1j.server.DatabaseFactory;
import l1j.server.server.IdFactory;
import l1j.server.server.model.Instance.L1ItemInstance;
import l1j.server.server.templates.L1Armor;
import l1j.server.server.templates.L1EtcItem;
import l1j.server.server.templates.L1Item;
import l1j.server.server.templates.L1Weapon;
import l1j.server.server.utils.SQLUtil;

public class ItemTable {
	private static Logger _log = Logger.getLogger(ItemTable.class.getName());

	private static final Map<String, Integer> _armorTypes = new HashMap<String, Integer>();

	private static final Map<String, Integer> _weaponTypes = new HashMap<String, Integer>();

	private static final Map<String, Integer> _weaponId = new HashMap<String, Integer>();

	private static final Map<String, Integer> _materialTypes = new HashMap<String, Integer>();

	private static final Map<String, Integer> _etcItemTypes = new HashMap<String, Integer>();

	private static final Map<String, Integer> _useTypes = new HashMap<String, Integer>();

	private static ItemTable _instance;

	private L1Item _allTemplates[];

	private final Map<Integer, L1EtcItem> _etcitems;

	private final Map<Integer, L1Armor> _armors;

	private final Map<Integer, L1Weapon> _weapons;

	private HashMap<Integer, String> _AllItems;//opqlo DropList資料加載

	static {

		_etcItemTypes.put("arrow", new Integer(0));
		_etcItemTypes.put("wand", new Integer(1));
		_etcItemTypes.put("light", new Integer(2));
		_etcItemTypes.put("gem", new Integer(3));
		_etcItemTypes.put("totem", new Integer(4));
		_etcItemTypes.put("firecracker", new Integer(5));
		_etcItemTypes.put("potion", new Integer(6));
		_etcItemTypes.put("food", new Integer(7));
		_etcItemTypes.put("scroll", new Integer(8));
		_etcItemTypes.put("questitem", new Integer(9));
		_etcItemTypes.put("spellbook", new Integer(10));
		_etcItemTypes.put("petitem", new Integer(11));
		_etcItemTypes.put("other", new Integer(12));
		_etcItemTypes.put("material", new Integer(13));
		_etcItemTypes.put("event", new Integer(14));
		_etcItemTypes.put("sting", new Integer(15));
		_etcItemTypes.put("treasure_box", new Integer(16));
		_etcItemTypes.put("magic_doll", new Integer(17));
		_etcItemTypes.put("ItemSummon", new Integer(18));//TODO 召喚道具
		_etcItemTypes.put("teleport_scroll", new Integer(19));
		_etcItemTypes.put("quest", new Integer(20));//opqlo 副本道具

		//ElixirStats
		_useTypes.put("ElixirStats", new Integer(-9));//萬能藥水
		_useTypes.put("dice", new Integer(-8));//魔法骰子
		_useTypes.put("magic_doll", new Integer(-7));//魔法娃娃
		_useTypes.put("pet_collar", new Integer(-6));//寵物項圈
		_useTypes.put("light", new Integer(-5));//照明類
		_useTypes.put("food", new Integer(-4));//肉類(飽食度物品)
		_useTypes.put("furniture", new Integer(-3));//家具
		_useTypes.put("hpr", new Integer(-2));// 恢復類道具
		_useTypes.put("none", new Integer(-1));// 使用不可能
		_useTypes.put("normal", new Integer(0));
		_useTypes.put("weapon", new Integer(1));
		_useTypes.put("armor", new Integer(2));
		// _useTypes.put("wand1", new Integer(3));
		// _useTypes.put("wand", new Integer(4));
		// ワンドを振るアクションをとる(C_RequestExtraCommandが送られる)
		_useTypes.put("spell_long", new Integer(5)); // 地面 / オブジェクト選択(遠距離)
		_useTypes.put("ntele", new Integer(6));
		_useTypes.put("identify", new Integer(7));
		_useTypes.put("res", new Integer(8));
		_useTypes.put("letter", new Integer(12));
		_useTypes.put("letter_w", new Integer(13));
		_useTypes.put("choice", new Integer(14));
		_useTypes.put("instrument", new Integer(15));
		_useTypes.put("sosc", new Integer(16));
		_useTypes.put("spell_short", new Integer(17)); // 地面 / オブジェクト選択(近距離)
		_useTypes.put("T", new Integer(18));
		_useTypes.put("cloak", new Integer(19));
		_useTypes.put("glove", new Integer(20));
		_useTypes.put("boots", new Integer(21));
		_useTypes.put("helm", new Integer(22));
		_useTypes.put("ring", new Integer(23));
		_useTypes.put("amulet", new Integer(24));
		_useTypes.put("shield", new Integer(25));
		_useTypes.put("guarder", new Integer(25));
		_useTypes.put("dai", new Integer(26));
		_useTypes.put("zel", new Integer(27));
		_useTypes.put("blank", new Integer(28));
		_useTypes.put("btele", new Integer(29));
		_useTypes.put("spell_buff", new Integer(30)); // オブジェクト選択(遠距離)
		// Ctrlを押さないとパケットが飛ばない？
		_useTypes.put("ccard", new Integer(31));
		_useTypes.put("ccard_w", new Integer(32));
		_useTypes.put("vcard", new Integer(33));
		_useTypes.put("vcard_w", new Integer(34));
		_useTypes.put("wcard", new Integer(35));
		_useTypes.put("wcard_w", new Integer(36));
		_useTypes.put("belt", new Integer(37));
//		_useTypes.put("spell_long2", new Integer(39)); // 地面 / オブジェクト選択(遠距離)
		// 5と同じ？
		_useTypes.put("earring", new Integer(40));
		_useTypes.put("fishing_rod", new Integer(42));
		_useTypes.put("runeword_left", new Integer(43));//TODO 右
		_useTypes.put("runeword_right", new Integer(44));//TODO 左
		_useTypes.put("runeword_middle", new Integer(45));//TODO 中
		_useTypes.put("del", new Integer(46));
		_useTypes.put("runeword_r_d", new Integer(47));//TODO 輔助左下
		_useTypes.put("runeword_l_d", new Integer(48));//TODO 輔助右下
		_useTypes.put("opqlo1", new Integer(49));//TODO 無裝備封包-1

		_armorTypes.put("none", new Integer(0));
		_armorTypes.put("helm", new Integer(1));
		_armorTypes.put("armor", new Integer(2));
		_armorTypes.put("T", new Integer(3));
		_armorTypes.put("cloak", new Integer(4));
		_armorTypes.put("glove", new Integer(5));
		_armorTypes.put("boots", new Integer(6));
		_armorTypes.put("shield", new Integer(7));
		_armorTypes.put("amulet", new Integer(8));
		_armorTypes.put("ring", new Integer(9));
		_armorTypes.put("belt", new Integer(10));
		_armorTypes.put("ring2", new Integer(11));
		_armorTypes.put("earring", new Integer(12));
		_armorTypes.put("guarder", new Integer(13));
		_armorTypes.put("runeword_left", new Integer(14));//TODO 右
		_armorTypes.put("runeword_right", new Integer(15));//TODO 左
		_armorTypes.put("runeword_middle", new Integer(16));//TODO 中
		_armorTypes.put("runeword_l_d", new Integer(17));//TODO 輔助右下
		_armorTypes.put("runeword_r_d", new Integer(18));//TODO 輔助左下
		_armorTypes.put("opqlo1", new Integer(19));//TODO 無裝備封包-1
		
		_weaponTypes.put("sword", new Integer(1));
		_weaponTypes.put("dagger", new Integer(2));
		_weaponTypes.put("tohandsword", new Integer(3));
		_weaponTypes.put("bow", new Integer(4));
		_weaponTypes.put("spear", new Integer(5));
		_weaponTypes.put("blunt", new Integer(6));
		_weaponTypes.put("staff", new Integer(7));
		_weaponTypes.put("throwingknife", new Integer(8));
		_weaponTypes.put("arrow", new Integer(9));
		_weaponTypes.put("gauntlet", new Integer(10));
		_weaponTypes.put("claw", new Integer(11));
		_weaponTypes.put("edoryu", new Integer(12));
		_weaponTypes.put("singlebow", new Integer(13));
		_weaponTypes.put("singlespear", new Integer(14));
		_weaponTypes.put("tohandblunt", new Integer(15));
		_weaponTypes.put("tohandstaff", new Integer(16));
		_weaponTypes.put("kiringku", new Integer(17));
		_weaponTypes.put("chainsword", new Integer(18));

		_weaponId.put("sword", new Integer(4));
		_weaponId.put("dagger", new Integer(46));
		_weaponId.put("tohandsword", new Integer(50));
		_weaponId.put("bow", new Integer(20));
		_weaponId.put("blunt", new Integer(11));
		_weaponId.put("spear", new Integer(24));
		_weaponId.put("staff", new Integer(40));
		_weaponId.put("throwingknife", new Integer(2922));
		_weaponId.put("arrow", new Integer(66));
		_weaponId.put("gauntlet", new Integer(62));
		_weaponId.put("claw", new Integer(58));
		_weaponId.put("edoryu", new Integer(54));
		_weaponId.put("singlebow", new Integer(20));
		_weaponId.put("singlespear", new Integer(24));
		_weaponId.put("tohandblunt", new Integer(11));
		_weaponId.put("tohandstaff", new Integer(40));
		_weaponId.put("kiringku", new Integer(58));
		_weaponId.put("chainsword", new Integer(24));

		_materialTypes.put("none", new Integer(0));
		_materialTypes.put("liquid", new Integer(1));//液體
		_materialTypes.put("web", new Integer(2));//蠟燭、網
		_materialTypes.put("vegetation", new Integer(3));//植物
		_materialTypes.put("animalmatter", new Integer(4));//動物
		_materialTypes.put("paper", new Integer(5));//紙張
		_materialTypes.put("cloth", new Integer(6));//布
		_materialTypes.put("leather", new Integer(7));//皮革
		_materialTypes.put("wood", new Integer(8));//木頭
		_materialTypes.put("bone", new Integer(9));//骨頭
		_materialTypes.put("dragonscale", new Integer(10));//龍鱗
		_materialTypes.put("iron", new Integer(11));//鐵
		_materialTypes.put("steel", new Integer(12));//鋼
		_materialTypes.put("copper", new Integer(13));//銅
		_materialTypes.put("silver", new Integer(14));//銀
		_materialTypes.put("gold", new Integer(15));//金
		_materialTypes.put("platinum", new Integer(16));//白金
		_materialTypes.put("mithril", new Integer(17));//米索莉(秘銀)
		_materialTypes.put("blackmithril", new Integer(18));//黑色米索莉(黑色秘銀)
		_materialTypes.put("glass", new Integer(19));//玻璃
		_materialTypes.put("gemstone", new Integer(20));//寶石
		_materialTypes.put("mineral", new Integer(21));//礦物
		_materialTypes.put("oriharukon", new Integer(22));//奧里哈魯根
	}

	public static ItemTable getInstance() {
		if (_instance == null) {
			_instance = new ItemTable();
		}
		return _instance;
	}

	private ItemTable() {
//		if(Config.DB_UPDATE){/**opqlo DB資料自動備註*/
			this._AllItems =new HashMap<Integer, String>();
//			}
		this._etcitems = this.allEtcItem();
		this._weapons = this.allWeapon();
		this._armors = this.allArmor();
		this.buildFastLookupTable();
	}

	private Map<Integer, L1Armor> allArmor() {
		final Map<Integer, L1Armor> result = new HashMap<Integer, L1Armor>();
		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		L1Armor armor = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("select * from armor");

			rs = pstm.executeQuery();
			while (rs.next()) {
				armor = new L1Armor();
				armor.setItemId(rs.getInt("item_id"));
				armor.setName(rs.getString("name"));
				armor.setUnidentifiedNameId(rs
						.getString("unidentified_name_id"));
				armor.setIdentifiedNameId(rs.getString("identified_name_id"));
				armor.setType(_armorTypes.get(rs.getString("type"))
						.intValue());
				// armor.setType1((_armorId
				// .get(rs.getString("armor_type"))).intValue()); // 使わない
				armor.setType2(2);
				armor.setUseType(_useTypes.get(rs.getString("type")).intValue());
				armor.setMaterial(_materialTypes.get(rs.getString("material")).intValue());
				armor.setWeight(rs.getInt("weight"));
				armor.setGfxId(rs.getInt("invgfx"));
				armor.setGroundGfxId(rs.getInt("grdgfx"));
				armor.setItemDescId(rs.getInt("itemdesc_id"));
				armor.set_ac(rs.getInt("ac"));
				armor.set_safeenchant(rs.getInt("safenchant"));
				armor.setUseRoyal(rs.getInt("use_royal") == 0 ? false : true);
				armor.setUseKnight(rs.getInt("use_knight") == 0 ? false : true);
				armor.setUseElf(rs.getInt("use_elf") == 0 ? false : true);
				armor.setUseMage(rs.getInt("use_mage") == 0 ? false : true);
				armor.setUseDarkelf(rs.getInt("use_darkelf") == 0 ? false : true);
				armor.setUseDragonknight(rs.getInt("use_dragonknight") == 0 ? false : true);
				armor.setUseIllusionist(rs.getInt("use_illusionist") == 0 ? false : true);
				armor.set_addstr(rs.getByte("add_str"));
				armor.set_addcon(rs.getByte("add_con"));
				armor.set_adddex(rs.getByte("add_dex"));
				armor.set_addint(rs.getByte("add_int"));
				armor.set_addwis(rs.getByte("add_wis"));
				armor.set_addcha(rs.getByte("add_cha"));
				armor.set_addhp(rs.getInt("add_hp"));
				armor.set_addmp(rs.getInt("add_mp"));
				armor.set_addhpr(rs.getInt("add_hpr"));
				armor.set_addmpr(rs.getInt("add_mpr"));
				armor.set_addsp(rs.getInt("add_sp"));
				armor.setMinLevel(rs.getInt("min_lvl"));
				armor.setMaxLevel(rs.getInt("max_lvl"));
				armor.set_mdef(rs.getInt("m_def"));
				armor.setDamageReduction(rs.getInt("damage_reduction"));
				armor.setWeightReduction(rs.getInt("weight_reduction"));
				armor.setHitModifierByArmor(rs.getInt("hit_modifier"));
				armor.setDmgModifierByArmor(rs.getInt("dmg_modifier"));
				armor.setBowHitModifierByArmor(rs.getInt("bow_hit_modifier"));
				armor.setBowDmgModifierByArmor(rs.getInt("bow_dmg_modifier"));
				armor.setHasteItem(rs.getInt("haste_item") == 0 ? false : true);
				armor.setBless(rs.getInt("bless"));
				armor.setTradable(rs.getInt("trade") == 0 ? true : false);
				armor.setCantDelete(rs.getInt("cant_delete") == 1 ? true: false);
				armor.set_defense_earth(rs.getInt("defense_earth"));
				armor.set_defense_water(rs.getInt("defense_water"));
				armor.set_defense_wind(rs.getInt("defense_wind"));
				armor.set_defense_fire(rs.getInt("defense_fire"));
				armor.set_regist_stun(rs.getInt("regist_stun"));
				armor.set_regist_stone(rs.getInt("regist_stone"));
				armor.set_regist_sleep(rs.getInt("regist_sleep"));
				armor.set_regist_freeze(rs.getInt("regist_freeze"));
				armor.set_regist_sustain(rs.getInt("regist_sustain"));
				armor.set_regist_blind(rs.getInt("regist_blind"));
				armor.setMaxUseTime(rs.getInt("max_use_time"));
				armor.setGrade(rs.getInt("grade"));//TODO 補上DB欄位
				armor.setBroad(rs.getBoolean("broad"));//sosodemon add 掉落物品廣播//eboy新增
				armor.setDelete_Day(rs.getInt("delete_day")); // 道具天數刪除系統(指定天數)
				armor.setDelete_Date(rs.getTimestamp("delete_date")); // 道具天數刪除系統(指定日期)
				armor.setExtraDmg(rs.getInt("ExtraDmg"));	//opqlo 額外攻擊力
				armor.setExtraDef(rs.getInt("ExtraDef"));	//opqlo 額外防禦力
				armor.addDroprate(rs.getDouble("掉寶率")); //opqlo 掉寶率
				result.put(new Integer(armor.getItemId()), armor);
//				if(Config.DB_UPDATE){/**opqlo DB資料自動備註*/
				this._AllItems.put(new Integer(armor.getItemId()), armor.getName()); //opqlo DropList資料加載
//				}
			}
		} catch (final NullPointerException e) {
			_log.log(Level.SEVERE, new StringBuilder().append(armor.getName())
					.append("(" + armor.getItemId() + ")").append("在讀取時失敗了。").toString());
		} catch (final SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);

		}
		return result;
	}

	private Map<Integer, L1EtcItem> allEtcItem() {
		final Map<Integer, L1EtcItem> result = new HashMap<Integer, L1EtcItem>();

		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		L1EtcItem item = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("select * from etcitem");

			rs = pstm.executeQuery();
			while (rs.next()) {
				item = new L1EtcItem();
				item.setItemId(rs.getInt("item_id"));
				item.setName(rs.getString("name"));
				item.setUnidentifiedNameId(rs.getString("unidentified_name_id"));
				item.setIdentifiedNameId(rs.getString("identified_name_id"));
				item.setType(_etcItemTypes.get(rs.getString("item_type")).intValue());
				item.setUseType(_useTypes.get(rs.getString("use_type")).intValue());
				// item.setType1(0); // 使わない
				item.setType2(0);
				item.setMaterial(_materialTypes.get(rs.getString("material")).intValue());
				item.setWeight(rs.getInt("weight"));
				item.setGfxId(rs.getInt("invgfx"));
				item.setGroundGfxId(rs.getInt("grdgfx"));
				item.setItemDescId(rs.getInt("itemdesc_id"));
				item.setMinLevel(rs.getInt("min_lvl"));
				item.setMaxLevel(rs.getInt("max_lvl"));
				item.setBless(rs.getInt("bless"));
				item.setTradable(rs.getInt("trade") == 0 ? true : false);
				item.setCantDelete(rs.getInt("cant_delete") == 1 ? true : false);
				item.setCanSeal(rs.getInt("can_seal") == 1 ? true : false);
				item.setDmgSmall(rs.getInt("dmg_small"));
				item.setDmgLarge(rs.getInt("dmg_large"));
				item.set_stackable(rs.getInt("stackable") == 1 ? true : false);
				item.setMaxChargeCount(rs.getInt("max_charge_count"));
				item.set_locx(rs.getInt("locx"));
				item.set_locy(rs.getInt("locy"));
				item.set_mapid(rs.getShort("mapid"));
				item.set_delayid(rs.getInt("delay_id"));
				item.set_delaytime(rs.getInt("delay_time"));
				item.set_delayEffect(rs.getInt("delay_effect"));
				item.setFoodVolume(rs.getInt("food_volume"));
				item.setToBeSavedAtOnce(rs.getInt("save_at_once") == 1 ? true: false);
				item.setBroad(rs.getBoolean("broad"));//sosodemon add 掉落物品廣播 //eboy新增
				item.setDelete_Day(rs.getInt("delete_day")); // 道具天數刪除系統(指定天數)
				item.setDelete_Date(rs.getTimestamp("delete_date")); // 道具天數刪除系統(指定日期)
				result.put(new Integer(item.getItemId()), item);
//				if(Config.DB_UPDATE){/**opqlo DB資料自動備註*/
				this._AllItems.put(new Integer(item.getItemId()), item.getName()); //opqlo DropList資料加載
//				}
			}
		} catch (final NullPointerException e) {
			_log.log(Level.SEVERE, new StringBuilder().append(item.getName())
					.append("(" + item.getItemId() + ")").append(
					"在讀取時失敗了。").toString());
		} catch (final SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);
		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);
		}
		return result;
	}

	private Map<Integer, L1Weapon> allWeapon() {
		final Map<Integer, L1Weapon> result = new HashMap<Integer, L1Weapon>();

		Connection con = null;
		PreparedStatement pstm = null;
		ResultSet rs = null;
		L1Weapon weapon = null;
		try {
			con = DatabaseFactory.get().getConnection();
			pstm = con.prepareStatement("select * from weapon");

			rs = pstm.executeQuery();
			while (rs.next()) {
				weapon = new L1Weapon();
				weapon.setItemId(rs.getInt("item_id"));
				weapon.setName(rs.getString("name"));
				weapon.setUnidentifiedNameId(rs.getString("unidentified_name_id"));
				weapon.setIdentifiedNameId(rs.getString("identified_name_id"));
				weapon.setType(_weaponTypes.get(rs.getString("type")).intValue());
				weapon.setType1(_weaponId.get(rs.getString("type")).intValue());
				weapon.setType2(1);
				weapon.setUseType(1);
				weapon.setMaterial(_materialTypes.get(rs.getString("material")).intValue());
				weapon.setWeight(rs.getInt("weight"));
				weapon.setGfxId(rs.getInt("invgfx"));
				weapon.setGroundGfxId(rs.getInt("grdgfx"));
				weapon.setItemDescId(rs.getInt("itemdesc_id"));
				weapon.setDmgSmall(rs.getInt("dmg_small"));
				weapon.setDmgLarge(rs.getInt("dmg_large"));
				weapon.setRange(rs.getInt("range"));
				weapon.set_safeenchant(rs.getInt("safenchant"));
				weapon.setUseRoyal(rs.getInt("use_royal") == 0 ? false : true);
				weapon.setUseKnight(rs.getInt("use_knight") == 0 ? false : true);
				weapon.setUseElf(rs.getInt("use_elf") == 0 ? false : true);
				weapon.setUseMage(rs.getInt("use_mage") == 0 ? false : true);
				weapon.setUseDarkelf(rs.getInt("use_darkelf") == 0 ? false: true);
				weapon.setUseDragonknight(rs.getInt("use_dragonknight") == 0 ? false : true);
				weapon.setUseIllusionist(rs.getInt("use_illusionist") == 0 ? false : true);
				weapon.setHitModifier(rs.getInt("hitmodifier"));
				weapon.setDmgModifier(rs.getInt("dmgmodifier"));
				weapon.set_addstr(rs.getByte("add_str"));
				weapon.set_adddex(rs.getByte("add_dex"));
				weapon.set_addcon(rs.getByte("add_con"));
				weapon.set_addint(rs.getByte("add_int"));
				weapon.set_addwis(rs.getByte("add_wis"));
				weapon.set_addcha(rs.getByte("add_cha"));
				weapon.set_addhp(rs.getInt("add_hp"));
				weapon.set_addmp(rs.getInt("add_mp"));
				weapon.set_addhpr(rs.getInt("add_hpr"));
				weapon.set_addmpr(rs.getInt("add_mpr"));
				weapon.set_addsp(rs.getInt("add_sp"));
				weapon.set_mdef(rs.getInt("m_def"));
				weapon.setDoubleDmgChance(rs.getInt("double_dmg_chance"));
				weapon.setMagicDmgModifier(rs.getInt("magicdmgmodifier"));
				weapon.set_canbedmg(rs.getInt("canbedmg"));
				weapon.setMinLevel(rs.getInt("min_lvl"));
				weapon.setMaxLevel(rs.getInt("max_lvl"));
				weapon.setBless(rs.getInt("bless"));
				weapon.setTradable(rs.getInt("trade") == 0 ? true : false);
				weapon.setCantDelete(rs.getInt("cant_delete") == 1 ? true: false);
				weapon.setHasteItem(rs.getInt("haste_item") == 0 ? false : true);
				weapon.setMaxUseTime(rs.getInt("max_use_time"));
				weapon.setBroad(rs.getBoolean("broad"));//sosodemon add 掉落武器物品廣播 //eboy新增
				weapon.setDelete_Day(rs.getInt("delete_day")); // 道具天數刪除系統(指定天數)
				weapon.setDelete_Date(rs.getTimestamp("delete_date")); // 道具天數刪除系統(指定日期)
				weapon.setExtraDmg(rs.getInt("ExtraDmg"));	//opqlo 額外攻擊力
				weapon.setExtraDef(rs.getInt("ExtraDef"));	//opqlo 額外防禦力
				result.put(new Integer(weapon.getItemId()), weapon);
//				if(Config.DB_UPDATE){/**opqlo DB資料自動備註*/
				this._AllItems.put(new Integer(weapon.getItemId()), weapon.getName()); //opqlo DropList資料加載
//				}
			}
		} catch (final NullPointerException e) {
			_log.log(Level.SEVERE, new StringBuilder().append(weapon.getName())
					.append("(" + weapon.getItemId() + ")").append(
					"在讀取時失敗了。。").toString());
		} catch (final SQLException e) {
			_log.log(Level.SEVERE, e.getLocalizedMessage(), e);

		} finally {
			SQLUtil.close(rs);
			SQLUtil.close(pstm);
			SQLUtil.close(con);

		}
		return result;
	}

	private void buildFastLookupTable() {
		int highestId = 0;

		final Collection<L1EtcItem> items = this._etcitems.values();
		for (final L1EtcItem item : items) {
			if (item.getItemId() > highestId) {
				highestId = item.getItemId();
			}
		}

		final Collection<L1Weapon> weapons = this._weapons.values();
		for (final L1Weapon weapon : weapons) {
			if (weapon.getItemId() > highestId) {
				highestId = weapon.getItemId();
			}
		}

		final Collection<L1Armor> armors = this._armors.values();
		for (final L1Armor armor : armors) {
			if (armor.getItemId() > highestId) {
				highestId = armor.getItemId();
			}
		}

		this._allTemplates = new L1Item[highestId + 1];

		for (final Integer id : this._etcitems.keySet()) {
			final L1EtcItem item = this._etcitems.get(id);
			this._allTemplates[id.intValue()] = item;
		}

		for (final Integer id : this._weapons.keySet()) {
			final L1Weapon item = this._weapons.get(id);
			this._allTemplates[id.intValue()] = item;
		}

		for (final Integer id : this._armors.keySet()) {
			final L1Armor item = this._armors.get(id);
			this._allTemplates[id.intValue()] = item;
		}
	}

	public L1ItemInstance createItem(final int itemId) {
		final L1Item temp = this.getTemplate(itemId);
		if (temp == null) {
			return null;
		}
		final L1ItemInstance item = new L1ItemInstance();
		item.setId(IdFactory.getInstance().nextId());
		item.setItem(temp);
		L1World.getInstance().storeObject(item);
		return item;
	}

	public int findItemIdByName(final String name) {
		int itemid = 0;
		for (final L1Item item : this._allTemplates) {
			if (item != null && item.getName().equals(name)) {
				itemid = item.getItemId();
				break;
			}
		}
		return itemid;
	}

	public int findItemIdByNameWithoutSpace(final String name) {
		int itemid = 0;
		for (final L1Item item : this._allTemplates) {
			if (item != null && item.getName().replace(" ", "").equals(name)) {
				itemid = item.getItemId();
				break;
			}
		}
		return itemid;
	}

	/**
	 * 用ITEMID找尋道具名稱 opqlo
	 * */
	public String findItemNameById(final int itemid) {
		//_AllItems.put(new Integer(item.getItemId()), item.getName()); //opqlo DropList資料加載

		if(this._AllItems.get(itemid) !=null){
			return this._AllItems.get(itemid);

		}else{
			return null;
		}
		/*for (L1Item item : _allTemplates) {
			if (item != null && itemid== item.getItemId()) {
				return item.getName();
			}else{
				return null;
			}
		}
			return null;
		 */
	}
	public L1Item getTemplate(final int id) {
		return this._allTemplates[id];
	}
}
